package com.platform.constants;

import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Description: 数据库配置字段
 *
 * @author zyk
 */
public class DBSettingField {

    public static final SettingField HOST = new SettingField("host","主机地址",true,null,null,true);

    public static final SettingField DB_NAME = new SettingField("dbName","数据库名称",true,null,null,true);

    public static final SettingField USER_NAME = new SettingField("userName","用户名",true,null,null,true);

    public static final SettingField PASSWORD = new SettingField("password","密码",true,null,null,false);

    public static class SettingField {

        private String field;

        private String desc;

        private boolean required;

        private String pattern;

        private String defaultValue;

        private boolean repeatFieldFlag;

        public SettingField(String field, String desc, boolean required, String pattern, String defaultValue, boolean repeatFieldFlag) {
            this.field = field;
            this.desc = desc;
            this.required = required;
            this.pattern = pattern;
            this.defaultValue = defaultValue;
            this.repeatFieldFlag = repeatFieldFlag;
        }

        public boolean isRepeatFieldFlag() {
            return repeatFieldFlag;
        }

        public void setRepeatFieldFlag(boolean repeatFieldFlag) {
            this.repeatFieldFlag = repeatFieldFlag;
        }

        public String getField() {
            return field;
        }

        public void setField(String field) {
            this.field = field;
        }

        public String getDesc() {
            return desc;
        }

        public void setDesc(String desc) {
            this.desc = desc;
        }

        public boolean isRequired() {
            return required;
        }

        public void setRequired(boolean required) {
            this.required = required;
        }

        public String getPattern() {
            return pattern;
        }

        public void setPattern(String pattern) {
            this.pattern = pattern;
        }

        public String getDefaultValue() {
            return defaultValue;
        }

        public void setDefaultValue(String defaultValue) {
            this.defaultValue = defaultValue;
        }
    }

    public static List<SettingField> values(Class clazz){
        Field[] fields = clazz.getFields();
        return Arrays.stream(fields).map(field -> {
            try {
                return  (SettingField) field.get(clazz);
            } catch (IllegalAccessException e) {
                return null;
            }
        }).collect(Collectors.toList());
    }
}
